﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using Microsoft.Phone.Net.NetworkInformation;

namespace Nok_Air
{
    public partial class MainPage : PhoneApplicationPage
    {
        private System.Windows.Controls.Primitives.Popup popup;
        private FirstPageMenu fpm = null;
        // Constructor
        public MainPage()
        {
            //Availble airport
            InitializeComponent();
            this.popup = new System.Windows.Controls.Primitives.Popup();
            this.popup.Child = new ProgressBarUserControl();
            this.popup.IsOpen = true;

            fpm  = new Wording().fpm;
            menuPanoHeader.Text = fpm.Menu;
            airportHeader.Text = fpm.Airport;
            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            } 
            this.popup.IsOpen = false;
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
        private void Menu_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Button bt = sender as Button;
			if (App.checkConnection())
			{
				switch (bt.Tag.ToString())
				{
					case "Check In":
						NavigationService.Navigate(new Uri("/CheckIn/1CheckInPage.xaml", UriKind.Relative));
						break;
					case "Flight Schedule":
						NavigationService.Navigate(new Uri("/FlightSchedule/FlightSchedulePage.xaml", UriKind.Relative));
						break;
					case "Book Flight":
						NavigationService.Navigate(new Uri("/BookFlight/1BookFlightPage.xaml", UriKind.Relative));
						break;
					case "View Booking":
						NavigationService.Navigate(new Uri("/ViewBooking/ViewBookingPage.xaml", UriKind.Relative));
						break;
				}
			}
        }

        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			if (!App.checkConnection())
			{
				menuPano.IsEnabled = false;
				airportPano.IsEnabled = false;
			}
            airplaneFly.Begin();
        }

        private void airPortListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
			if (App.checkConnection())
			{
				if (airPortListBox.SelectedIndex == -1)
					return;
				NavigationService.Navigate(new Uri("/BingMap/AirPortBingMap.xaml?selectedItem=" + airPortListBox.SelectedIndex, UriKind.RelativeOrAbsolute));
				airPortListBox.SelectedIndex = -1;
			}
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            //if (!NavigationService.CanGoBack)
            {
                var result = MessageBox.Show(fpm.Exit, "", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }
    }
}